package com.elitedatai.mchtest.manage.repository;

import com.elitedatai.mchtest.manage.model.entity.DeviceHmsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Set;

public interface DeviceHmsRepository extends JpaRepository<DeviceHmsEntity, Long> {

    @Query("SELECT d FROM DeviceHmsEntity d WHERE " +
            "(:snList IS NULL OR d.sn IN :snList) AND " +
            "(:beginTime IS NULL OR :endTime IS NULL OR d.createTime BETWEEN :beginTime AND :endTime) AND " +
            "(:updateTime IS NULL OR d.updateTime = :updateTime) AND " +
            "(:level IS NULL OR d.level = :level) AND " +
            "(:lang = 'zh' AND :message IS NOT NULL AND d.messageZh LIKE %:message%) OR " +
            "(:lang = 'en' AND :message IS NOT NULL AND d.messageEn LIKE %:message%)")
    Page<DeviceHmsEntity> findWithFilters(
            @Param("snList") Set<String> snList,
            @Param("beginTime") Long beginTime,
            @Param("endTime") Long endTime,
            @Param("updateTime") Long updateTime,
            @Param("level") Integer level,
            @Param("lang") String lang,
            @Param("message") String message,
            Pageable pageable);

    @Query("SELECT d FROM DeviceHmsEntity d WHERE d.sn = :deviceSn AND d.updateTime = 0")
    DeviceHmsEntity findByDeviceSnAnd0UpdateTime(String deviceSn);
}